package core.testes.codeGeneration;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import junit.framework.TestCase;

import core.codeGeneration.CodeGenerator;
import core.error.ErrorHandler;
import core.lexical.LexicalAnalyser;
import core.syntatic.SyntaticAnalyser;

public class ArithmeticCodeGeneratorTest extends TestCase{

	private SyntaticAnalyser parser;
	
	private final String fileName = "examples\\arithmeticCodeGeneratorTest.ford";
	
	private final String codeGeneratedFile = "codeFiles\\arithmeticCodeGeneratorTest Intermediary Code.txt";
	
	private BufferedReader bufReader;

	private FileReader reader;
	
	@Override
	protected void setUp() throws Exception {
				
		FileReader source = new FileReader( new File( fileName ) );
		
		 parser = new SyntaticAnalyser(	new LexicalAnalyser( source, new ErrorHandler() ) );
		 CodeGenerator.getInstace().init(fileName.substring(9, fileName.length() - 5));
	}
	
	@Override
	protected void tearDown() throws Exception {
		reader.close();
		bufReader.close();
		super.tearDown();
	}
	
	public void testArithmeticCodeGeneration(){
		parser.analyseIt();
		 
		try {
			reader = new FileReader(new File(codeGeneratedFile));
			
			bufReader = new BufferedReader(reader);
			
			assertEquals(bufReader.readLine(), "1.	[ T1 * c d ]");
			assertEquals(bufReader.readLine(), "2.	[ T2 + b T1 ]");
			assertEquals(bufReader.readLine(), "3.	[ T3 / T2 10 ]");
			assertEquals(bufReader.readLine(), "4.	[ a = T3 ]");
			assertEquals(bufReader.readLine(), "5.	[ T4 -U 10 ]");
			assertEquals(bufReader.readLine(), "6.	[ T5 * T4 f ]");
			assertEquals(bufReader.readLine(), "7.	[ T6 + a T5 ]");
			assertEquals(bufReader.readLine(), "8.	[ T7 - 5 T6 ]");
			assertEquals(bufReader.readLine(), "9.	[ e = T7 ]");

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		 
		
	}
}
